﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FortuneHeaven.Web.Common;
using System.Text;
using FortuneHeavenCommon.Enum;
using FortuneHeavenCommon.ComMethod;
using FortuneHeavenBAL;
using FortuneHeavenCommon.DataMap;

namespace FortuneHeaven.Web.Pages.Shop
{
    public partial class GiftPurchase : BasePage
    {
        public int typeid;
        protected void Page_Load(object sender, EventArgs e)
        {
            SetPageHeader(FortuneHeavenCommon.Enum.MainPageTabType.gift);
            typeid = 0;
            int.TryParse(Request["type"], out typeid);
            StringBuilder builder = new StringBuilder();

            this.usPager.PagerChanged += new Web.Controls.SePagination.PagerChangeHandler(usPager_PagerChanged);
            uclogin.LoginSuccess += new Web.Controls.LoginPanel.LoginSuccessHandler(uclogin_LoginSuccess);
            foreach (GiftType type in Enum.GetValues(typeof(GiftType)))
            {
                if (typeid == (int)type)
                {
                    builder.Append(string.Format("<a class=\"tl\" href=\"javascript://\">{0}</a>", UtilMethod.GetDescription(type)));
                }
                else
                {
                    builder.Append(string.Format("<a  href=\"GiftPurchase.aspx?type={0}\">{1}</a>", (int)type, UtilMethod.GetDescription(type)));
                }
            }
            this.ltHead.Text = builder.ToString().TrimEnd('|');
            if (!IsPostBack)
            {
                if (AuthorityCenter.CurrentUserID > 0)
                {
                    ShowUser();
                }
                else
                {
                    ShowLogin();
                }
                this.usPager.PageSize = 12;
                ResetList(typeid);
            }
        }

        void uclogin_LoginSuccess()
        {
            ShowUser();
        }

        private void ShowUser()
        {
            divHeadpic.Visible = true;
            uclogin.Visible = false;
            SetUserInfor();
        }
        private void ShowLogin()
        {
            divHeadpic.Visible = false;
            uclogin.Visible = true;
        }

        public void SetUserInfor()
        {
            ltUserName.Text = AuthorityCenter.CurrentUserName;
            UserInforBO bo = new UserInforBO();
            UserInfor infor = bo.GetUserInfor(AuthorityCenter.CurrentUserID);
            GameScoreInfoBO scoreinfo = new GameScoreInfoBO();
            GameScoreInfo game = scoreinfo.GetGameScore(AuthorityCenter.CurrentUserID);
            
            //ltFortune.Text = infor.FortuneValue.ToString();
            if (!string.IsNullOrEmpty(infor.ShowImg))
            {
                imgHead.Src = GetPagesURL(FortuneHeavenCommon.ComMethod.CommonPath.GameShow, infor.ShowImg);
            }
            ltFortune.Text = game!=null? game.Coupon.ToString():"0";
        }

        void usPager_PagerChanged(object sender, int currentpage)
        {
            GiftBO gift = new GiftBO();
            this.rpGift.DataSource = gift.GetGiftsByType(this.usPager.CurrentPageIndex, this.usPager.PageSize, typeid);
            rpGift.DataBind();
        }

        public void ResetList(int type)
        {
            GiftBO gift = new GiftBO();
            this.usPager.RecordCount = gift.GetTotalSizeByType(type);
            this.usPager.CurrentPageIndex = 1;
        }

        public void PurchaseHead(object sender, EventArgs e)
        {
            LinkButton button = sender as LinkButton;

            if (AuthorityCenter.CurrentUserID <= 0)
            {
                ShowCurError("请先登陆");
                //ClientScript.RegisterStartupScript(this.GetType(), "infor", "alert('请先登陆!');", true);
                //Response.Write("<script> alert('请先登陆!');document.location ='" + Page.ResolveUrl("~/Login.aspx") + "'</script>");
            }
            else
            {
                string giftid = button.CommandArgument;
                GiftBO bo = new GiftBO();
                int id = 0;
                int.TryParse(giftid, out id);
                Gift gift = bo.GetInfo(id);
                GameScoreInfoBO scoreinfo = new GameScoreInfoBO();
                GameScoreInfo game = scoreinfo.GetGameScore(AuthorityCenter.CurrentUserID);
                int current = (game != null ? game.Coupon : 0);
                if (current < gift.Price)
                {
                    ShowCurError("奖券不够!");
                }
                else
                {
                    Response.Redirect(string.Format("GiftOrderAddress.aspx?id={0}&pre={1}", gift.ID, Server.UrlEncode(Request.Url.OriginalString)));
                }
            }
        }
    }
}